#!/usr/bin/python3
# coding: utf-8


import sys
import os
import requests

auth_refresh_url = 'https://access.video.qq.com/user/auth_refresh?vappid=11059694&vsecret=fdf61a6be0aad57132bc5cdf78ac30145b6cd2c1470b0cfe&type=qq&g_tk=1493769093&g_vstk=1951926823&g_actk=574009123&callback=jQuery191040420867410362793_1594375166759&_=1594375166760'
sckey = os.environ['sckey']

ftqq_url = "https://sc.ftqq.com/%s.send"%(sckey)
url1 = 'https://vip.video.qq.com/fcgi-bin/comm_cgi?name=hierarchical_task_system&cmd=2'
url2 = 'https://v.qq.com/x/bu/mobile_checkin'

login_headers = {
    'Referer': 'https://v.qq.com',
    'Cookie': 'pgv_pvi=7759970304; RK=r3J1E9MXTB; ptcz=da5c93647b22deaaeacc060748b6d795eb1ad961380ad69099447bab755253ea; pgv_pvid=9256767026; o_cookie=1327438873; pac_uid=1_1327438873; XWINDEXGREY=0; tvfe_boss_uuid=19f6aff8f05dd9ea; video_guid=2d8b1ee6ab01a9a8; video_platform=2; main_login=qq; vqq_access_token=5BDCFCA9C0571A438D20A2F61E0163D4; vqq_appid=101483052; vqq_openid=A7A15EDA4AE45C149445FEE5ADDCDC58; vqq_vuserid=157362773; vqq_refresh_token=20718E7BED61601D6F7FAFA18D273AD3; iip=0; pgv_si=s2009409536; pgv_info=ssid=s4320540734; uin=o1327438873; skey=@zdaFMDCRU; vqq_vusession=Emyb7w4rG6jqkSa_tWh29Q..; vqq_next_refresh_time=6600; vqq_login_time_init=1594375140; login_time_last=2020-7-10 17:59:0; uid=250640454'
}

login = requests.get(auth_refresh_url, headers=login_headers)
cookie = requests.utils.dict_from_cookiejar(login.cookies)

if not cookie:
    print ("auth_refresh error")
    payload = {'text': '腾讯视频V力值签到通知', 'desp': '获取Cookie失败，Cookie失效'}
    requests.post(ftqq_url, params=payload)

sign_headers = {
    'Cookie': 'pgv_pvi=7759970304; RK=r3J1E9MXTB; ptcz=da5c93647b22deaaeacc060748b6d795eb1ad961380ad69099447bab755253ea; pgv_pvid=9256767026; o_cookie=1327438873; pac_uid=1_1327438873; XWINDEXGREY=0; tvfe_boss_uuid=19f6aff8f05dd9ea; video_guid=2d8b1ee6ab01a9a8; video_platform=2; main_login=qq; vqq_access_token=5BDCFCA9C0571A438D20A2F61E0163D4; vqq_appid=101483052; vqq_openid=A7A15EDA4AE45C149445FEE5ADDCDC58; vqq_vuserid=157362773; vqq_refresh_token=20718E7BED61601D6F7FAFA18D273AD3; iip=0; pgv_si=s2009409536; pgv_info=ssid=s4320540734; uin=o1327438873; skey=@zdaFMDCRU; vqq_vusession=Emyb7w4rG6jqkSa_tWh29Q..; vqq_next_refresh_time=6600; vqq_login_time_init=1594375140; login_time_last=2020-7-10 17:59:0; uid=250640454' + cookie['vqq_vusession'] + ';',
    'Referer': 'https://m.v.qq.com'
}
def start():
  sign1 = requests.get(url1,headers=sign_headers).text
  if 'Account Verify Error' in sign1:
    print( 'Sign1 error,Cookie Invalid')
    status = "链接1 失败，Cookie失效"
  else:
    print ('Sign1 Success')
    status = "链接1 成功，获得V力值：" + sign1[42:-14]
    print(status)

  sign2 = requests.get(url2,headers=sign_headers).text
  if 'Unauthorized' in sign2:
    print ('Sign2 error,Cookie Invalid')
    status = status + "\n\n 链接2 失败，Cookie失效"
  else:
    print ('Sign2 Success')
    status = status + "\n\n 链接2 成功"

  payload = {'text': '腾讯视频V力值签到通知', 'desp': status}
  requests.post(ftqq_url, params=payload)

def main_handler(event, context):
  return start()
if __name__ == '__main__':
  start()
